// Load Vehicles

private ["_procedureName", "_parameters", "_missionid", "_response", "_landVehicleCountInDB", "_serverData", "_countInDB", "_z", "_thisID", "_landVehicleData", "_vObject", "_tmp", "_thisVehicle", "_vPosition", "_vDir", "_vUp", "_vDam", "_vFuel", "_vLocked", "_vWeaponCargo", "_vMagazineCargo","_vEngine"];

_missionid = _this select 0;

// START load the landVehicle data

// Count the number of land vehicles associated with this mission id
_procedureName = "CountLandVehicleIDsByMission"; 
_parameters = format["[tmid=%1]",_missionid];

//	diag_log ("callExtension->Arma2NETMySQL: CountLandVehicleIDsByMission");		
_response = [_procedureName,_parameters] call persistent_fnc_callDatabase;	
//    diag_log ["callExtension->Arma2NETMySQL: CountLandVehicleIDsByMission _response: ",  _response, typeName _response];

if (pdb_log_enabled) then {
	diag_log format["SERVER MSG: SQL output: %1", _parameters];
};

_landVehicleCountInDB = _response select 0;    // copy the returned row into array
if (pdb_log_enabled) then {
	diag_log format ["SERVER MSG: Loading %1 Vehicles from database.",  _landVehicleCountInDB];
};

_serverData = format["Getting land vehicles from database..."];
PDB_SERVER_LOADERSTATUS = [_serverData]; publicVariable "PDB_SERVER_LOADERSTATUS";

// now get the vehicledata per id

_procedureName = "GetLandVehicleByInitid"; 

_countInDB = parseNumber (_landVehicleCountInDB select 0);

// START LOOP
for [{_z=0},{_z < _countInDB},{_z=_z+1}] do {

	_thisID = _z+1;
	
	_parameters = format["[tintid=%1,tmid=%2]", _thisID,_missionid];
	
	//	diag_log ("callExtension->Arma2NETMySQL: GetLandVehicleByInitid");		
	_response = [_procedureName,_parameters] call persistent_fnc_callDatabase;	
	//diag_log ["callExtension->Arma2NETMySQL: GetLandVehicleByInitid _response: ",  _response, typeName _response];
		
	_landVehicleData	= _response select 0;    // copy the returned row into array
	
	//	diag_log ["GetLandVehicleByInitid _landVehicleData: ",  _landVehicleData, typeName _landVehicleData];
	
	_vObject =_landVehicleData select 1;
	
	//diag_log ["_vObject: ",  _vObject, typeName _vObject];
	
	// Convert string to Object
	//	private "_tmp";
	//	call compile format ["_tmp = %1",_vObject];
	//	_thisVehicle = _tmp;

		//diag_log format ["_thisVehicle: %1 ",  _thisVehicle];
		diag_log format["Loading land vehicle: %1...", _vObject];
		//_serverData = format["Loading land vehicle: %1...", _thisVehicle];
		//PDB_SERVER_LOADERSTATUS = [_serverData]; publicVariable "PDB_SERVER_LOADERSTATUS";
		
		_vPosition =_landVehicleData select 2;
		_vPosition = [_vPosition, "|", ","] call CBA_fnc_replace; 
		_vPosition = call compile _vPosition;
		
		//									diag_log ["_vPosition: ",  _vPosition, typeName _vPosition];
		
		_vDir = _landVehicleData select 3;
		_vDir = [_vDir, "|", ","] call CBA_fnc_replace;
		_vDir = call compile _vDir;
		
		//										diag_log ["_vDir: ",  _vDir, typeName _vDir];
		
		_vUp = _landVehicleData select 4;
		_vUp = [_vUp, "|", ","] call CBA_fnc_replace;
		_vUp = call compile _vUp;
		
		//											diag_log ["_vUp: ",  _vUp, typeName _vUp];
		
		_vDam = _landVehicleData select 5;
        _vDam = [_vDam, ",", "."] call CBA_fnc_replace; 
		_vDam = parseNumber _vDam;
		
		_vFuel = _landVehicleData select 6;
		_vFuel = parseNumber _vFuel;
        
		_vLocked = _landVehicleData select 7;
		
		if (_vLocked == "false") then { _vLocked = false; } else{ _vLocked = true; };
		//										 diag_log ["_vLocked: ",  _vLocked, typeName _vLocked];
		
		_vEngine = _landVehicleData select 9;
		if (_vEngine == "false") then { _vEngine = false; } else{ _vEngine = true; };
		//										diag_log ["_vEngine: ",  _vEngine, typeName _vEngine];

		
		
        _thisVehicle = createVehicle [_vObject, _vPosition, [], 0, "NONE"];
        _thisVehicle setVehicleVarName _vObject;
        _thisVehicle addeventhandler ["HandleDamage", {_this call persistent_fnc_vehicleDamage;}];
        
        if (pdb_objects_contents_enabled) then {
			clearWeaponCargoGlobal _thisVehicle;
			_vWeaponCargo = _landVehicleData select 8;
			_vWeaponCargo = [_vWeaponCargo, "|", ","] call CBA_fnc_replace; 
			_vWeaponCargo = call compile _vWeaponCargo;	
		
			//									diag_log ["_vWeaponCargo: ",  _vWeaponCargo, typeName _vWeaponCargo];
			clearMagazineCargoGlobal _thisVehicle;
			_vMagazineCargo = _objectData select 10;
			_vMagazineCargo = [_vMagazineCargo, "|", ","] call CBA_fnc_replace; 
			_vMagazineCargo = call compile _vMagazineCargo;	
		} else {
			_vWeaponCargo = [];
			_vMagazineCargo = [];
		};
        
        _locatedDamages = _objectData select 11;
        diag_log format ["loadVehicule 1 : %1", _locatedDamages];
        if (_locatedDamages != "") then {
             diag_log format ["loadVehicule 1.5 : %1", _locatedDamages];
            _varToSave = [];
	        _damages = [_locatedDamages, "|"] call CBA_fnc_split;
            diag_log format ["loadVehicule 2 : %1", _damages];
	        {
                _var = [_x, "="] call CBA_fnc_split;
                _name = _var select 0;
                _val = _var select 1;
                _val = parseNumber _val;
                diag_log format ["loadVehicule 3 : %1 = %2", _name, _val];
                _varToSave set [(count _varToSave), _name];
	            _thisVehicle setVariable [_name, _val];
	        } forEach (_damages);
            _varToSave = _vehicle getVariable "varToSaved";
            diag_log format ["loadVehicule 4 : %1", _varToSave];
        };
        
        
		// set the vehicles position
		//_thisVehicle setPosATL _vPosition;
		// set the vehicles direction
		_thisVehicle setVectorDirAndUp [_vDir,_vUp];
		// set the vehicles damage
		_thisVehicle setDammage _vDam;
		// set the vehicles fuel 
		_thisVehicle setFuel _vFuel;
		// set the vehicles lock 
		_thisVehicle lock _vLocked;
		// set the vehicles WeaponCargo 
		for "_i" from 0 to (count (_vWeaponCargo select 0)-1) do {
				_thisVehicle addWeaponCargoGlobal [(_vWeaponCargo select 0) select _i, (_vWeaponCargo select 1) select _i];
		};
		for "_m" from 0 to (count (_vMagazineCargo select 0)-1) do {
				_thisVehicle addMagazineCargoGlobal [(_vMagazineCargo select 0) select _m, (_vMagazineCargo select 1) select _m];
		};
		// set the vehicles engine 
		_thisVehicle engineOn _vEngine;

	
};